package com.rongxue.controller.payment;

import com.rongxue.pojo.ObjectResult;
import com.rongxue.pojo.XMLBean;
import com.rongxue.service.MobileCodeService;
import com.rongxue.service.UserAccountService;
import com.rongxue.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yangweijian on 2018/1/23.
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private MobileCodeService mobileCodeService;

    /**
     * 用户充值提现记录
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/money_records", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult money_records(XMLBean xmlBean) {
        ObjectResult objectResult = new ObjectResult();
        try {
             objectResult = userAccountService.selectWalletRecordsByMap(xmlBean);
        }
        catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }

    /**
     * 用户可用优惠券
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/use_rewards")
    @ResponseBody
    public ObjectResult use_rewards(XMLBean xmlBean) {
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = userAccountService.selectUseRewardsByMap(xmlBean);
        }
        catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }
    @RequestMapping(value = "/sms_code")
    @ResponseBody
    public ObjectResult sms_code(XMLBean xmlBean, HttpServletRequest request) {
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        try {
            String ip = CodeUtils.getIpAddress(request);
            return  mobileCodeService.sms_code(xmlBean,ip);
        }catch (Exception e){
            e.printStackTrace();
            return  ObjectResult.build("0","短信发送失败");
        }
    }

    /**
     * 资金明细
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/capital_subsidiary", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult capital_subsidiary(XMLBean xmlBean){
        ObjectResult objectResult = new ObjectResult();
        try {
            objectResult = userAccountService.selectWalletRecordsByMap(xmlBean);
        }
        catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return objectResult;
    }
}
